<template>
	<view class="wrap">
		<view class="top">
			<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account" label-width="120"
					:label-position="labelPosition" label="姓名" prop="username">
					<u-input :border="border" placeholder="请输入姓名" v-model="model.username" type="text"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="性别" prop="sex">
					<u-input :border="border" type="select" :select-open="actionSheetShow" v-model="model.sex"
						placeholder="请选择性别" @click="actionSheetShow = true"></u-input>
				</u-form-item>
				<u-form-item :rightIconStyle="{color: '#888', fontSize: '32rpx'}" right-icon="kefu-ermai"
					:label-position="labelPosition" label="手机号码" prop="tel" label-width="150">
					<u-input :border="border" placeholder="请输入手机号" v-model="model.tel" type="number"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="所在地区" prop="place" label-width="150">
					<u-input :border="border" type="select" :select-open="pickerShow" v-model="model.place"
						placeholder="请选择地区" @click="pickerShow = true"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<u-button :ripple="true" ripple-bg-color="#909399" class="btn" type="success" shape="circle" @click="open">保存
		</u-button>
		<u-modal v-model="show" :content="content"></u-modal>
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-select mode="single-column" :list="selectList" v-model="selectShow" @confirm="selectConfirm"></u-select>
		<u-picker mode="region" v-model="pickerShow" @confirm="placeConfirm"></u-picker>
	</view>
</template>

<script>
	import {
		userupdate
	} from '@/common/api/user.js'
	import {
		setUser,
		getUser
	} from '@/common/entity/user.js'
	export default {
		data() {
			return {
				model: {
					id: "",
					username: '',
					sex: '',
					tel: '',
					place: ''
				},
				selectList: [{
						value: '广西桂林农场',
						label: '广西桂林农场'
					},
					{
						value: '广西北海农场',
						label: '广西北海农场'
					},
					{
						value: '云南洱海花园',
						label: '云南洱海花园'
					}
				],

				show: false,
				content: '信息修改成功！',
				rules: {
					name: [{
							required: true,
							message: '请输入姓名',
							trigger: 'blur',
						},
						{
							min: 3,
							max: 5,
							message: '姓名长度在3到5个字符',
							trigger: ['change', 'blur'],
						},
						{
							// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.chinese(value);
							},
							message: '姓名必须为中文',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						},
					],
					sex: [{
						required: true,
						message: '请选择性别',
						trigger: 'change'
					}],
					place: [{
						required: true,
						message: '请选择地区',
						trigger: 'change',
					}],
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						}
					]
				},
				border: false,
				check: false,
				selectStatus: 'close',
				checkboxList: [{
						name: '荔枝',
						checked: false,
						disabled: false
					},
					{
						name: '香蕉',
						checked: false,
						disabled: false
					},
					{
						name: '橙子',
						checked: false,
						disabled: false
					},
					{
						name: '草莓',
						checked: false,
						disabled: false
					}
				],
				radioList: [{
						name: '滴灌',
						checked: true,
						disabled: false
					},
					{
						name: '滴灌喷灌',
						checked: false,
						disabled: false
					},
					{
						name: '人工灌溉',
						checked: false,
						disabled: false
					}
				],
				actionSheetList: [{
						text: '男'
					},
					{
						text: '女'
					},
					{
						text: '保密'
					}
				],
				actionSheetShow: false,
				pickerShow: false,
				selectShow: false,
				radioCheckWidth: 'auto',
				radioCheckWrap: false,
				labelPosition: 'left',
				codeTips: '',
				errorType: ['message'],
			};
		},
		methods: {
			placeConfirm(e) {
				this.model.place = e.province.label + '-' + e.city.label + '-' + e.area.label;
			},
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.model.sex = this.actionSheetList[index].text;
			},
			open() {
				// console.log(this.model)
				userupdate(this.model).then(res => {
					if (res[1].data.result) {
						setUser(res[1].data.data)
						this.$u.toast("更新成功！")
					}
				})
			}
		},
		mounted() {
			let user = getUser()
			this.model = user
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}

	.btn {
		margin-top: 20rpx;
		width: 80vw;
	}
</style>
